<template>
  <div>
    <div class="r-header">
      <div class="header-item">
        <el-radio-group
          size="small"
          v-model="segmentType"
          style="margin-bottom: 10px"
        >
          <el-radio-button v-if="activeName == 'autoIndex'" label="0"
            >按小时</el-radio-button
          >
          <el-radio-button v-if="activeName == 'autoIndex'" label="1"
            >按日</el-radio-button
          >
          <el-radio-button label="2">按月</el-radio-button>
        </el-radio-group>
      </div>
      <div class="header-item">
        <el-date-picker
          size="small"
          v-model="daysDate"
          type="daterange"
          value-format="yyyy-MM-dd"
          format="yyyy-MM-dd"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        >
        </el-date-picker>
      </div>
      <div class="header-item">
        <span>评价项目：</span>
        <el-select
          size="small"
          @change="selectChange"
          v-model="bussinessData"
          multiple
          collapse-tags
          style="margin-left: 20px"
          placeholder="请选择"
        >
          <el-option
            v-for="item in bussinessItemList"
            :key="item.itemCode"
            :label="item.itemName"
            :value="item.itemCode"
          >
          </el-option>
        </el-select>
      </div>
    </div>
    <div class="header-btns">
      <el-button
        class="query-btn"
        type="primary"
        icon="el-icon-search"
        size="mini"
        @click="clickQueryHandle()"
        >查询</el-button
      >
      <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
        >重置</el-button
      >
      <!-- <el-button type="warning" icon="el-icon-download" size="mini" plain
                v-hasPermi="['system:user:export']">导出</el-button> -->
    </div>
    <div>
      <el-table ref="multipleTable" :data="tableData" tooltip-effect="dark">
        <el-table-column label="序号">
          <template slot-scope="scope">{{ scope.$index + 1 }}</template>
        </el-table-column>
        <el-table-column label="点位名称">
          <template slot-scope="scope">{{ scope.row.segmentName }}</template>
        </el-table-column>
        <el-table-column prop="lakeName" label="湖库名称"> </el-table-column>
        <el-table-column
          prop="sampleTime"
          label="监测时间"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column
          prop="waterQualityIndexclassification"
          label="营养状态级别"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column
          prop="waterQualityIndex"
          label="综合营养状态指数TLI（）"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column
          v-for="(item, index) in businessList"
          :key="index"
          :label="item.itemName"
          show-overflow-tooltip
        >
          <template slot-scope="scope"
            >{{ scope.row[item.itemCode + "_val"] }} /
            {{ scope.row[item.itemCode + "_int"] }}</template
          >
        </el-table-column>
      </el-table>
      <el-pagination
        layout="total,sizes,prev, pager, next"
        :total="total"
        background
        :current-page="pageNum"
        :page-size="pageSize"
        :page-sizes="[10, 20, 50]"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      ></el-pagination>
    </div>
  </div>
</template>
<script>
import {
  bussinessItemData,
  nutritionalAutoIndexlist,
  nutritionalManualIndexlist,
} from "@/api/dataResources/waterJcData/sgMonitor";
import { watch } from "vue";
export default {
  name: "rightTable",
  props: {
    activeName: {
      type: String,
      default: "autoIndex",
    },
  },
  watch: {
    activeName(v) {
      if (v == "autoIndex") {
        this.segmentType = "0";
      } else {
        this.segmentType = "2";
      }
    },
    segmentType(v) {
      let endDate, startDate;
      endDate = $moment().format("{yyyy}-{mm}-{dd}");

      if (v == "0") {
        startDate = $moment(endDate)
          .offset("day", -3)
          .format("{yyyy}-{mm}-{dd}");
      } else if (v == "1") {
        startDate = $moment(endDate)
          .offset("day", -7)
          .format("{yyyy}-{mm}-{dd}");
      } else {
        startDate = $moment(endDate)
          .offset("month", -6)
          .format("{yyyy}-{mm}-{dd}");
      }
      this.daysDate = [startDate, endDate];
    },
  },

  data() {
    return {
      segmentType: "0",
      daysDate: [],
      bussinessData: [],
      bussinessItemList: [],
      tableData: [],
      businessList: [],
      checkedTreeNodes: [],
      total: 0,
      pageNum: 1,
      pageSize: 10,
    };
  },
  created() {
    this.daysDate = [
      $moment().format("{yyyy}-{mm}-{dd}"),
      $moment().offset("day", -3).format("{yyyy}-{mm}-{dd}"),
    ];
    // this.getBussinessItemData()
  },
  methods: {
    handleSizeChange(val) {
      this.pageNum = 1;
      this.pageSize = val;
      this.handleQuery();
    },
    handleCurrentChange(val) {
      this.pageNum = val;
      this.handleQuery();
    },
    async queryData() {
      // await this.getBussinessItemData();
      await this.handleQuery();
    },
    selectedItems() {
      return this.bussinessData.map((itemCode) => {
        const item = this.bussinessItemList.find(
          (option) => option.itemCode === itemCode
        );
        return item;
      });
    },
    selectChange() {
      this.handleQuery();
    },
    resetQuery() {
      this.bussinessData = "";
      this.daysDate = [];
      this.segmentType = "0";
    },
    clickQueryHandle() {
      this.handleQuery();
    },
    async handleQuery(nodes) {
      await this.getBussinessItemData();
      if (nodes) {
        this.checkedTreeNodes = [...nodes];
      }
      this.businessList = this.selectedItems();
      let params = this.getParams();
      if (this.activeName === "autoIndex") {
        nutritionalAutoIndexlist(params).then((res) => {
          this.tableData = res.rows;
          this.total = res.total;
        });
      } else {
        nutritionalManualIndexlist(params).then((res) => {
          if (res.code == 200) {
            this.tableData = res.rows;
            this.total = res.total;
          }
        });
      }
    },
    getParams() {
      let params = {
        pageNum: this.pageNum,
        pageSize: this.pageSize,
        segmentType: this.segmentType,
        beginTime: this.parseTime(this.daysDate[0], "{yyyy}-{mm}-{dd}"),
        endTime: this.parseTime(this.daysDate[1], "{yyyy}-{mm}-{dd}"),
        bussinessCategoryCode: this.activeName == "autoIndex" ? "Auto" : "CBN",
      };
      params.segmentId = this.checkedTreeNodes
        .filter((item) => item.pId !== "0")
        .map((item) => item.id)
        .join(",");
      params.itemCode = this.bussinessData.join(",");
      return params;
    },
    getBussinessItemData() {
      if (this.bussinessItemList.length) {
        return;
      }
      return bussinessItemData({ bussinessCategoryCode: "NI" }).then((res) => {
        if (res.code == 200) {
          this.bussinessItemList = res.data;
          this.bussinessData = res.data.map((item) => item.itemCode);
        }
      });
    },
  },
};
</script>
<style lang="scss" scoped>
.r-header {
  display: flex;
  justify-content: left;
}

.header-item {
  margin-right: 10px;
}
</style>
